//
//  QSTabBarController.swift
//  TestSwift
//
//  Created by Song on 2018/4/29.
//  Copyright © 2018 Song. All rights reserved.
//

import UIKit

class QSTabBarController: UITabBarController {
    private let titleArray = ["11111", "22222", "22222"]
    private let imgArrary = ["icon_menu_news_off", "icon_menu_news_off", "icon_menu_news_off"]
    private let selectedImgArray = ["icon_menu_news_on", "icon_menu_news_on", "icon_menu_news_on"]
    private let vcArray = [ViewController11111(), ViewController22222(), ViewController33333()]
    
    // tintColor
    private let itemColor = UIColor.black
    private let barBgColor = UIColor.yellow
    
    // MARK: - Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tabBar.isTranslucent = false
        self.tabBar.tintColor = self.itemColor
        self.tabBar.barTintColor = barBgColor
        guard self.titleArray.count > 0, self.imgArrary.count > 0, self.selectedImgArray.count > 0 else {
            return
        }
        
        // 设置子控制器
        self.qs_setSubViewControllers()
    }
    
    /// 点击item时调用，用代码直接设置不会调用此方法
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        // 选中的item下标
        let itemIndex = tabBar.items?.index(of: item)
        print(itemIndex ?? 0)
    }
    
    // MARK: - Private Methods
    /// 设置子控制器
    func qs_setSubViewControllers(){
        var index: Int = 0
        for vc in vcArray {
            // 创建导航控制器
            let nav = UINavigationController.init(rootViewController: vc)
            self.addChildViewController(nav)
            
            let title = titleArray[index]
            let imgName = imgArrary[index]
            let selImgName = selectedImgArray[index]
            
            let img = UIImage.init(named: imgName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
            let selImg = UIImage.init(named: selImgName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
            
            let item = UITabBarItem.init(title: title, image: img, selectedImage: selImg)
            nav.tabBarItem = item
            
            index += 1
        }
    }
}
